package com.kj.xuyuan.mine;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kj.xuyuan.R;
import com.kj.xuyuan.base.BaseFragment;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.base.Constants;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.UserInfoBean;
import com.kj.xuyuan.util.GlideUtils;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.SPUtils;
import com.kj.xuyuan.util.StringUtils;
import com.kj.xuyuan.util.ToastUtil;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.Map;

import androidx.constraintlayout.widget.ConstraintLayout;
import butterknife.BindView;
import butterknife.OnClick;

public class MineFragment extends BaseFragment {
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.tv_copy)
    TextView tvCopy;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.cl_balance)
    ConstraintLayout clBalance;
    @BindView(R.id.tv_wallet)
    TextView tvWallet;
    @BindView(R.id.cl_wallet)
    ConstraintLayout clWallet;
    @BindView(R.id.cl_user)
    ConstraintLayout clUser;
    @BindView(R.id.tv_info_degree_of_completion)
    TextView tvInfoDegreeOfCompletion;
    @BindView(R.id.cl_edit_info)
    ConstraintLayout clEditInfo;
    @BindView(R.id.tv_friend_degree_of_completion)
    TextView tvFriendDegreeOfCompletion;
    @BindView(R.id.cl_make_friend)
    ConstraintLayout clMakeFriend;
    @BindView(R.id.ll_my_dynamic)
    LinearLayout llMyDynamic;
    @BindView(R.id.ll_my_auth)
    LinearLayout llMyAuth;
    @BindView(R.id.ll_invite_get_money)
    LinearLayout llInviteGetMoney;
    @BindView(R.id.ll_task_center)
    LinearLayout llTaskCenter;
    @BindView(R.id.ll_matchmaker)
    LinearLayout llMatchmaker;
    @BindView(R.id.ll_level)
    LinearLayout llLevel;
    @BindView(R.id.ll_vip)
    LinearLayout llVip;
    @BindView(R.id.ll_honor)
    LinearLayout llHonor;
    @BindView(R.id.ll_honor_vip)
    LinearLayout llHonorVip;
    @BindView(R.id.ll_question)
    LinearLayout llQuestion;
    @BindView(R.id.ll_set)
    LinearLayout llSet;

    private String balance;
    private String changeMoneyScale;
    private String nickName;
    private int level;

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getUserData();
    }

    private void getUserData() {
        Map map = new HashMap();
        map.put("id", SPUtils.getIntData(getContext(), Constants.UID,0)+"");

        OkGoUtil.postRequest(getContext(), UrlConstants.GET_USER_INFO, map, new JsonCallback<BaseModel<UserInfoBean>>() {
            @Override
            public void onSuccess(Response<BaseModel<UserInfoBean>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        setView(response.body().getData());
                    }
                    ToastUtil.showToast(getContext(),response.body().getMsg());
                }
            }

            @Override
            public void onError(Response<BaseModel<UserInfoBean>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    private void setView(UserInfoBean data) {
        tvName.setText(data.getNickname());
        nickName = data.getNickname();
        tvSex.setText(data.getYears());
        Drawable drawable = null;
        if(data.getGender() == 1){//男
            tvSex.setBackgroundResource(R.drawable.shape_blue_20);
            drawable = getResources().getDrawable(R.mipmap.icon_boy);
        }else if(data.getGender() == 2){//女
            tvSex.setBackgroundResource(R.drawable.shape_pink_corners_30);
            drawable = getResources().getDrawable(R.mipmap.icon_girl);
        }else{
            tvSex.setVisibility(View.INVISIBLE);
        }
        drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
        tvSex.setCompoundDrawables(drawable,null,null,null);

        if(!StringUtils.isEmpty(data.getProvince()) && !StringUtils.isEmpty(data.getCity())){
            tvLocation.setText(data.getProvince() +" "+ data.getCity());
        }else{
            tvLocation.setVisibility(View.GONE);
        }

        tvId.setText("絮缘号："+data.getIdno());
        tvInfoDegreeOfCompletion.setText("已完成"+data.getEdit_rate()+"%");
        tvWallet.setText(data.getMoney()+"元");
        tvBalance.setText(data.getScore()+"钻石");
        balance = data.getScore();
        changeMoneyScale = data.getChange_money();
        level = data.getLevel();

        GlideUtils.loadCircleImage(getContext(),UrlConstants.Base_Http+data.getAvatar(),ivHead);
        SPUtils.setStringData(getContext(),Constants.WX_ACCOUNT,data.getWx_num());
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tv_copy, R.id.cl_balance, R.id.cl_wallet, R.id.cl_user, R.id.cl_edit_info, R.id.cl_make_friend, R.id.ll_my_dynamic, R.id.ll_my_auth, R.id.ll_invite_get_money, R.id.ll_task_center, R.id.ll_matchmaker, R.id.ll_level, R.id.ll_vip, R.id.ll_honor, R.id.ll_honor_vip, R.id.ll_question, R.id.ll_set})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_copy:
                break;
            case R.id.cl_balance:
                ChargeActivity.actionStar(getContext(),balance,changeMoneyScale);
                break;
            case R.id.cl_wallet:
                break;
            case R.id.cl_user:
                HomePageActivity.actionStar(getContext(),SPUtils.getIntData(getContext(),Constants.UID,0));
                break;
            case R.id.cl_edit_info:
                EditUserInfoActivity.actionStar(getContext());
                break;
            case R.id.cl_make_friend:
                MakeFriendRequestActivity.actionStar(getContext());
                break;
            case R.id.ll_my_dynamic:
                MyDynamicActivity.actionStar(getContext());
                break;
            case R.id.ll_my_auth:
                AuthCenterActivity.actionStar(getContext());
                break;
            case R.id.ll_invite_get_money:
                InviteFriendActivity.actionStar(getContext());
                break;
            case R.id.ll_task_center:
                TaskCenterActivity.actionStar(getContext());
                break;
            case R.id.ll_matchmaker:
                break;
            case R.id.ll_level:
                break;
            case R.id.ll_vip:
                VipCenterActivity.actionStar(getContext(),level,nickName);
                break;
            case R.id.ll_honor:
                break;
            case R.id.ll_honor_vip:
                break;
            case R.id.ll_question:
                break;
            case R.id.ll_set:
                SetActivity.actionStar(getContext());
                break;
        }
    }
}
